/**
 * TODO:
 *  match pattern in manifest.json
 */
/**
 * Color Scheme ref:
 * http://www.colorschemer.com/schemes/
 */
function focusColor(r, v, y) {
    var cs = [
        "#FEF461",
        "#FFE100",
        "#F5C012",
        "#F57D27",
        "#E5301F",
        "#DB4A1E"
    ];

    var cs = [
        "#FFB3D9",    
        "#FF99CC",    
        "#FF73B3",    
        "#FF4D99",    
        "#FF2680",    
        "#FF0066"    
    ];
    var score = 0

    score = parseFloat(r);
    score *= Math.log(v);
    score -= (new Date()).getFullYear() - y;
    score = Math.round(score * cs.length / 100) - 1;

    if (score < 0) {
        score = 0;
    }

    return cs[score];
}



function highlight() {
    var pat = /.*imdb:\s+([\d\.]+)\/10\s+\(([\d,]+)\svotes\).*发行日期:\s(\d{4})年.*/i;
    var spans = document.getElementsByClassName('snippet');
    if (spans.length < 1) {
        return false;
    }

    console.log("working on feed");
    for (var i = 0;i < spans.length;i++) {
        if (spans[i].className.split(/\s+/).indexOf('highlighted') != -1) {
            continue;
        }
        m = pat.exec(spans[i].textContent);
        if (m != null) {
            spans[i].parentNode.parentNode.style.backgroundColor = focusColor(m[1], m[2].replace(",", ""), parseInt(m[3]));
            spans[i].className += " highlighted";
        }
    }

}

function checkReadingFeed() {
    if (href != document.getElementById("chrome-title").childNodes[0].href) {
        position = -1;
        setTimeout(checkReadingFeed, 2000);
        return false;
    } 

    if (document.getElementById('entries').scrollHeight < position) {
        position = -1;
    }

    if (document.getElementById('entries').scrollHeight > position) {
        position = document.getElementById('entries').scrollHeight;
        highlight();
    } 

    setTimeout(checkReadingFeed, 2000);
}


var position = -1;
var href = "http://www.verycd.com/sto/movie/";
var reading_href = "";

setTimeout(checkReadingFeed, 2000);

